home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 6584 / 6584.xpi / chrome / flashvideodownloader / content / flashvideodownloader.js < prev    next >
Text File  |  2010-01-24  |  23KB  |  875 lines

  1. var FlashVD = {
  2.  
  3.     options : null,
  4.     cache: null, // video, audio, adult urls internal persistent storage
  5.  
  6.     // categorized urls
  7.     // should be taken from http://flashvideodownloader.org/fvd-suite/sites/video.php
  8.     video : [],
  9.     audio : [],
  10.     adult : [],
  11.  
  12.     inpage_sites : [ 'santabanta','google','hulu','collegehumor','metacafe','dailymotion','dada','redtube','youtube' ],
  13.  
  14.     read_reg_string : function (a, b)
  15.     {
  16.         //var c = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  17.         try 
  18.         {
  19.             return FlashVD.options.getComplexValue(a, Components.interfaces.nsISupportsString).data;
  20.         } 
  21.         catch(e) 
  22.         {
  23.             return b;
  24.         }    
  25.     },
  26.  
  27.     write_reg_string : function (a, b) 
  28.     {
  29.         //var c = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  30.         try 
  31.         {
  32.             var d = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
  33.             d.data = b;
  34.             FlashVD.options.setComplexValue(a, Components.interfaces.nsISupportsString, d);
  35.             return true;
  36.         } 
  37.         catch(e) 
  38.         {
  39.             return false;
  40.         }        
  41.     },            
  42.  
  43.     showLic : function()
  44.     {
  45.         window.openDialog("chrome://flashvideodownloader/content/lic.xul", "lic", "width=" + (window.screen.availWidth/2 - 133) + ",height=" + (window.screen.availHeight/2) + ",chrome,modal,centerscreen");    
  46.     },
  47.  
  48.     init : function()
  49.     {
  50.         try
  51.         {
  52.             if (!FlashVD.options)
  53.                 FlashVD.options = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("FlashVD.options.");
  54.  
  55.             if (!FlashVD.cache)
  56.                 FlashVD.cache = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("FlashVD.cache.");
  57.  
  58.  
  59.             if (FlashVD.video.length == 0)
  60.                 FlashVD.loadUrlsFromCache("video", FlashVD.video);
  61.             if (FlashVD.audio.length == 0)
  62.                 FlashVD.loadUrlsFromCache("audio", FlashVD.audio);
  63.             if (FlashVD.adult.length == 0)
  64.                 FlashVD.loadUrlsFromCache("adult", FlashVD.adult);
  65.  
  66.             //FlashVD.updateCache();
  67.  
  68.         }
  69.         catch (e)
  70.         {
  71.             //alert("init\n" + e);
  72.         }
  73.  
  74.         try
  75.         {
  76.             var is_it_first = FlashVD.options.getBoolPref('flash_fr');
  77.         }
  78.         catch(e)
  79.         {
  80.             FlashVD.options.setBoolPref('flash_fr', true);
  81.             //FlashVD.showLic();
  82.         }
  83.     },
  84.  
  85.     updateCacheByTimer : function()
  86.     {
  87.         try
  88.         {
  89.             updated = FlashVD;
  90.             var curDateTime = new Date();
  91.             var curDay  = 13;//curDateTime.getDate();
  92.             var curHour = curDateTime.getHours();
  93.  
  94.             //var curState = curDay*24 + curHour;    // hourly
  95.             var curState = Math.floor(curDay/2);    // daily
  96.  
  97.             // simple update - only if hour/day changed or more
  98.             var lastState = curState - 1;
  99.             try { lastState = FlashVD.cache.getIntPref("state"); } catch(e) {}
  100.  
  101.             if (curState != lastState)
  102.             {
  103.                 //alert("curState=" + curState + "\nlastState=" + lastState);
  104.  
  105.                 FlashVD.updateCache();
  106.             }
  107.             FlashVD.cache.setIntPref("state", curState);
  108.         }
  109.         catch (e)
  110.         {
  111.             //alert("updateCacheByTimer\n" + e);
  112.         }
  113.     },
  114.  
  115.  
  116.     updateCache : function()
  117.     {
  118.         try
  119.         {    
  120.             FlashVD.requestUrls("video", FlashVD.video);
  121.             //FlashVD.requestUrls("audio", FlashVD.audio);
  122.             FlashVD.requestUrls("adult", FlashVD.adult);
  123.         }
  124.         catch (e)
  125.         {
  126.             //alert("updateCache\n" + e);
  127.         }
  128.     },
  129.  
  130.     processTextUrls : function (category, urls, text_urls)
  131.     {
  132.         try
  133.         {
  134.             var arr = text_urls.split("\n");
  135.             if (arr.length == 0)
  136.                 return;
  137.  
  138.             //alert("processTextUrls " + category + " - arr.length = " + arr.length + "\n" + arr.toString());
  139.  
  140.             // reset urls
  141.             urls.length = 0;
  142.  
  143.             for(var i = 0; i < arr.length; i++)
  144.             {
  145.                 var url = arr[i];
  146.  
  147.                 if (url.length < 8)// "http://".length)
  148.                     continue;
  149.                 if (url.indexOf("http://www.") == 0)
  150.                     url = url.substring(11);
  151.                 else if (url.indexOf("http://") == 0)
  152.                     url = url.substring(7);
  153.  
  154.                 urls.push(url);
  155.             }
  156.             //alert("processTextUrls " + category + " - urls.length = " + urls.length + "\n" + urls.toString());
  157.         }
  158.         catch(e)
  159.         {
  160.             //alert("processTextUrls() failed.\n" + e);
  161.         }
  162.     },
  163.  
  164.     loadUrlsFromCache : function (category, urls)
  165.     {
  166.         try
  167.         {    
  168.             // 1. Load text_urls from cache
  169.             var text_urls = null;
  170.             try{text_urls = FlashVD.cache.getCharPref(category); } catch(e) {}
  171.             if (text_urls == null || text_urls == "") // cache is empty
  172.             {
  173.                 // request urls from site - async cache update
  174.                 FlashVD.requestUrls(category, urls);
  175.                 return;
  176.             }
  177.  
  178.             //alert("loadUrlsFromCache\n" + text_urls);
  179.  
  180.             // 2. Load data to urls
  181.             FlashVD.processTextUrls(category, urls, text_urls);
  182.         }
  183.         catch (e)
  184.         {
  185.             //alert("loadUrlsFromCache\n" + e);
  186.         }
  187.     },
  188.  
  189.     processResponse : function (category, urls, text_urls)
  190.     {
  191.         try
  192.         {    
  193.             // 1. Save new text_urls to cache
  194.             FlashVD.cache.setCharPref(category, text_urls);
  195.  
  196.             //alert("processResponse\n" + text_urls);
  197.  
  198.             // 2. Load data to urls
  199.             FlashVD.processTextUrls(category, urls, text_urls);
  200.         }
  201.         catch (e)
  202.         {
  203.             //alert("processResponse\n" + e);
  204.         }
  205.     },
  206.  
  207.     requestUrls : function (category, urls_array)
  208.     {
  209.         try
  210.         {
  211.             var req = new XMLHttpRequest();
  212.             req.open('GET', 'http://flashvideodownloader.org/fvd-suite/sites/' + category + '.php', true);
  213.             req.onreadystatechange = function (aEvt)
  214.             {
  215.                 if (req.readyState == 4)
  216.                 {
  217.                     if (req.status == 200)
  218.                         //alert(req.responseText);
  219.                         FlashVD.processResponse(category, urls_array, req.responseText);
  220.                     //else
  221.                         //alert("Error loading http://flashvideodownloader.org/fvd-suite/sites/video.php");
  222.                 }
  223.             };
  224.             req.send(null);
  225.         }
  226.         catch (e)
  227.         {
  228.             //alert("requestUrls\n" + e);
  229.         }
  230.     },
  231.  
  232.     ila_load : function()
  233.     {
  234.         FlashVD.init();
  235.         FlashVD.updateCacheByTimer();
  236.  
  237.         document.getElementById("contentAreaContextMenu").addEventListener("popupshowing", FlashVD.init_cont_items, false);
  238.         try
  239.         {
  240.             var hotk = FlashVD.options.getCharPref('hotkey');
  241.             var hotm = FlashVD.options.getCharPref('hot_mod');
  242.         }
  243.         catch(e)
  244.         {
  245.             FlashVD.options.setCharPref('hotkey', 'L');
  246.             FlashVD.options.setCharPref('hot_mod', 'alt any control');
  247.         }
  248.  
  249.         try
  250.         {
  251.             var is_it_first = FlashVD.options.getBoolPref('flashvideodownloader');
  252.         }
  253.         catch(e)
  254.         {
  255.             FlashVD.options.setBoolPref('flashvideodownloader', true);
  256.             FlashVD.set_first_time_icons();
  257.         }
  258.     },
  259.  
  260.     set_first_time_icons : function()
  261.     {
  262.         try
  263.         {
  264.             var toolbox = document.getElementById("navigator-toolbox");
  265.             var toolboxDocument = toolbox.ownerDocument;
  266.             var has_ila_tool_icon = false;
  267.  
  268.             for (var i = 0; i < toolbox.childNodes.length; ++i) {
  269.                 var toolbar = toolbox.childNodes[i];
  270.                 if (toolbar.localName == "toolbar" && toolbar.getAttribute("customizable")=="true"){
  271.                 if(toolbar.currentSet.indexOf("ila_tool_icon") > -1) hasDeliciousButton = true;
  272.                 }
  273.             }
  274.             if(!has_ila_tool_icon) {
  275.                 var toolbar = document.getElementById("nav-bar");
  276.                 var newSet = "";
  277.                 var child = toolbar.firstChild;
  278.                 while(child){
  279.                     //Order of buttons is important
  280.                     if(!has_ila_tool_icon && child.id == "urlbar-container") {
  281.                         newSet += "ila_tool_icon,";
  282.                         has_ila_tool_icon = true;
  283.                     }
  284.                     newSet += child.id+",";
  285.                     child = child.nextSibling;
  286.                 }
  287.                 newSet = newSet.substring(0, newSet.length-1);
  288.                 toolbar.currentSet = newSet;
  289.                 toolbar.setAttribute("currentset", newSet);
  290.                 toolboxDocument.persist(toolbar.id, "currentset");
  291.                 try {
  292.                     BrowserToolboxCustomizeDone(true);                    
  293.                 }catch (e){}
  294.             }
  295.         }
  296.         catch(e)
  297.         {
  298.             //alert("set_first_time_icons\n" + e);
  299.         }
  300.     },
  301.  
  302.     ila_hotkey_f : function ()
  303.     {
  304.         FlashVD.DownloadLink();
  305.     },
  306.  
  307.     address_has : function (urls, params)
  308.     {
  309.         try
  310.         {
  311.             var addr = window.content.document.location.host;
  312.  
  313.             for(var i = 0; i < urls.length; i++)
  314.             {
  315.                 var url = urls[i];
  316.  
  317.                 if (addr.indexOf(url) >= 0)
  318.                 {
  319.                     var addr = window.content.document.URL;
  320.  
  321.                     if (addr.indexOf("http://www.") == 0)
  322.                         addr = addr.substring(11);
  323.                     else if (addr.indexOf("http://") == 0)
  324.                         addr = addr.substring(7);
  325.                     else if (addr.indexOf("www.") == 0)
  326.                         addr = addr.substring(4);
  327.  
  328.                     //alert(addr + "\n" + url);
  329.  
  330.                     if (addr.length - 1 <= url.length)
  331.                         params.isMainPage = true;
  332.  
  333.                     return true;
  334.                 }
  335.             }
  336.             return false;
  337.         }
  338.         catch (e)
  339.         {
  340.             //alert("address_has\n" + e);
  341.             return false;
  342.         }
  343.     },
  344.  
  345.     set_icon : function ()
  346.     {
  347.         try
  348.         {
  349.             FlashVD.init();
  350.  
  351.             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  352.  
  353.             try
  354.             {
  355.                 var addr = window.content.document.location.host;
  356.             }
  357.             catch(e)
  358.             {
  359.                 return;
  360.             }
  361.  
  362.             var i_stat = false;
  363.             //var parts = adr.match(/([^.]+)/ig);
  364.             var tmp;
  365.  
  366.             //while(tmp=parts.pop())
  367.             //{
  368.             //    if(FlashVD.sites.indexOf(tmp) != -1)
  369.             //    {
  370.             //        i_stat=true;
  371.             //        break;
  372.             //    }
  373.             //}
  374.  
  375.             //if(tmp=='google'){if(parts.pop()!='video')i_stat=false;}
  376.             //if(tmp=='myspace'){if(parts.pop()!='vids')i_stat=false;}
  377.             //if(tmp=='streetfire'){if(parts.pop()!='videos')i_stat=false;}
  378.  
  379.             //if (FlashVD.video.length > 0)
  380.             //    alert("FlashVD.video:\n" + FlashVD.video.toString());
  381.  
  382.             var params = {isMainPage:null};
  383.  
  384.             var has_audio = FlashVD.address_has(FlashVD.audio, params);
  385.             var has_video = FlashVD.address_has(FlashVD.video, params);
  386.             var has_adult = FlashVD.address_has(FlashVD.adult, params);
  387.  
  388.             var icon=document.getElementById('ila_tool_icon');
  389.  
  390.             i_stat = has_audio || has_video || has_adult;
  391.  
  392.             try 
  393.             {
  394.                 icon.setAttribute("adult_onpage", (has_adult ? "1" : "0"));
  395.                 icon.setAttribute("is_main_page", (params.isMainPage ? "1" : "0"));
  396.             }
  397.             catch (e) {}
  398.  
  399.             if(i_stat)
  400.             {
  401.                 try
  402.                 {
  403.                     icon.setAttribute("video_onpage", "0");
  404.                     icon.setAttribute("isset_in_db", "1");
  405.                     icon.setAttribute("tooltiptext", "Download video or game");
  406.                 }
  407.                 catch(e){}
  408.                 document.getElementById('flashvideodownloader-status').setAttribute("isset_in_db", "1");
  409.                 document.getElementById('flashvideodownloader-tooltip-value').setAttribute("value", "Download video or game");
  410.             }
  411.             else
  412.             {
  413.                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  414.                 var embs = window.content.document.getElementsByTagName('embed');
  415.                 var onpage_status = false;
  416.                 if(embs.length>0)
  417.                 {
  418.                     for(var i=0; i < embs.length; i++)
  419.                     {    
  420.                         var emb = embs[i];
  421.                         var partss = emb.src.match(/([^.]+)/ig);
  422.                         while(tmp = partss.pop())
  423.                         {
  424.                             if(FlashVD.inpage_sites.indexOf(tmp)!=-1)
  425.                             {
  426.                                 onpage_status=true;
  427.                                 break;
  428.                             }
  429.                         }
  430.                         if(onpage_status)break;
  431.                     }
  432.                 }
  433.  
  434.                 // Searching href
  435.                 if (!onpage_status)
  436.                 {
  437.                     var mediaPattern="mpg|mpeg|mp3|mp4|avi|rm|wmv|mov|flv|swf";
  438.                     var mPatt=new RegExp("^.*\\.(?:"+mediaPattern+")$","i");
  439.                     var dom=window.content.document;//window.content.document.documentElement;
  440.                     var Nodes = [];
  441.                     var xpr=dom.evaluate(".//a[@href]", dom, null,
  442.                         5, // == Components.interfaces.nsIDOMXPathResult.ORDERED_NODE_ITERATOR_TYPE,
  443.                         null);
  444.                     var node0=xpr.iterateNext();
  445.                     while(node0!=null)
  446.                     {
  447.                         var url=node0.getAttribute("href");
  448.                         if(mPatt.exec(url)!=null)
  449.                         {
  450.                             onpage_status=true;
  451.                             break;
  452.                         }
  453.                         node0=xpr.iterateNext();
  454.                     }
  455.  
  456.                     // Searching src
  457.                     if (!onpage_status)
  458.                     {
  459.                         xpr=dom.evaluate(".//embed[@src]", dom, null,
  460.                             5, // == Components.interfaces.nsIDOMXPathResult.ORDERED_NODE_ITERATOR_TYPE,
  461.                             null);
  462.                         node0=xpr.iterateNext();
  463.                         while(node0!=null)
  464.                         {
  465.                             var url=node0.getAttribute("src");
  466.                             if(mPatt.exec(url)!=null)
  467.                             {
  468.                                 onpage_status=true;
  469.                                 break;
  470.                             }
  471.                             node0=xpr.iterateNext();
  472.                         }
  473.                     }
  474.                 }
  475.  
  476.                 if(onpage_status==true)
  477.                 {
  478.                     icon.setAttribute("video_onpage", "1");
  479.                     try
  480.                     {
  481.                         icon.setAttribute("isset_in_db", "1");
  482.                         icon.setAttribute("tooltiptext", "Download video or game");
  483.                     }catch(e){}
  484.                     document.getElementById('flashvideodownloader-status').setAttribute("isset_in_db", "1");
  485.                     document.getElementById('flashvideodownloader-tooltip-value').setAttribute("value", "Download video or game");
  486.                 }
  487.                 else
  488.                 {
  489.                     icon.setAttribute("video_onpage", "0");
  490.                     try
  491.                     {
  492.                         icon.removeAttribute("isset_in_db");
  493.                         //icon.setAttribute("tooltiptext", "Click to ask us add this service");
  494.                         icon.setAttribute("tooltiptext", "");
  495.                     }catch(e){}
  496.                     document.getElementById('flashvideodownloader-status').removeAttribute("isset_in_db");
  497.                     //document.getElementById('flashvideodownloader-tooltip-value').setAttribute("value", "Click to ask us add this service");
  498.                     document.getElementById('flashvideodownloader-tooltip-value').setAttribute("value", "");
  499.                 }
  500.             }
  501.         }
  502.         catch (e)
  503.         {
  504.             //alert("set_icon\n" + e);
  505.         }
  506.     },
  507.  
  508.     init_cont_items : function ()
  509.     {
  510.         var adr=window.content.document.location;
  511.         try
  512.         {
  513.             if(adr.host=="www.flashvideodownloader.org" && adr.pathname=="/download.php")
  514.             {
  515.                 if(gContextMenu.onLink)
  516.                 {
  517.                     document.getElementById('save_tmf_menu').hidden=false;
  518.                     document.getElementById('flashvideodownloader-context').hidden=true;
  519.                 }
  520.             }
  521.             else
  522.             {
  523.                 document.getElementById('save_tmf_menu').hidden=true;
  524.                 document.getElementById('flashvideodownloader-context').hidden=false;
  525.             }
  526.         }
  527.         catch(e){document.getElementById('save_tmf_menu').hidden=true;}
  528.     },
  529.  
  530.     save_to_my_folder : function(durl) 
  531.     {
  532.         var folder;
  533.         try
  534.         {
  535.             folder = FlashVD.options.getComplexValue ("favorite-folder", Components.interfaces.nsISupportsString);
  536.         }
  537.         catch (e) 
  538.         {
  539.             alert('Please set your Favorite folder!');
  540.             return;
  541.         }
  542.         var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
  543.         var url;
  544.         if(durl==''){url=gContextMenu.linkURL;}else{url=durl;}
  545.         var uriObject = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService).newURI(url, null, null);
  546.         var fileObject = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
  547.         try
  548.         {
  549.             fname = getDefaultFileName(null, uriObject, null, null);
  550.         }
  551.         catch(e)
  552.         {
  553.             fname = getDefaultFileName(null, null, uriObject, null);
  554.         }
  555.         if(fname=='get_video')
  556.         {
  557.             var time = new Date();
  558.             var month = time.getMonth()+1;
  559.             fname=time.getFullYear()+'-'+month+'-'+time.getDate()+'_'+time.getHours()+'-'+time.getMinutes()+'-'+time.getSeconds()+'.mp4';
  560.         }
  561.         if(fname=='videoplayback')
  562.         {
  563.             var time = new Date();
  564.             var month = time.getMonth()+1;
  565.             fname=time.getFullYear()+'-'+month+'-'+time.getDate()+'_'+time.getHours()+'-'+time.getMinutes()+'-'+time.getSeconds()+'.flv';
  566.         }
  567. /*    fileObject.initWithPath(folder);
  568.     var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(Components.interfaces.nsIFilePicker);
  569.     fp.init(window,'save as', Components.interfaces.nsIFilePicker.modeSave);
  570.     fp.appendFilters(Components.interfaces.nsIFilePicker.filterAll);
  571.     fp.defaultString = fname;
  572.     fp.displayDirectory = fileObject;
  573.  
  574.     var res = fp.show();
  575.  
  576.     if ((res == Components.interfaces.nsIFilePicker.returnOK) || (res == Components.interfaces.nsIFilePicker.returnReplace)){
  577.         fname = fp.file.leafName;
  578.         folder = fp.file.path.substring(0, fp.file.path.length - fname.length);
  579.     }
  580.     else{return;}
  581. */    
  582.         try
  583.         {
  584.             fileObject.initWithPath(folder);
  585.         }
  586.         catch(e)
  587.         {
  588.             alert('This folder not set.');
  589.             return;
  590.         }
  591.         fileObject.append(fname);
  592.         try
  593.         {
  594.             var acObject = new AutoChosen(fileObject, uriObject);
  595.             internalSave(url, null, fname, null, null, false, null, acObject, null, false);
  596.         }
  597.         catch(e)
  598.         {
  599.             saveURL(url, fileObject, null, false, false, null);
  600.         }
  601.     },
  602.  
  603.     DownloadLink : function ()
  604.     {
  605.         try
  606.         {
  607.             //FlashVD.updateCacheByTimer();
  608.  
  609.             if(window.content.document.location=='about:blank') return true;
  610.  
  611.             if(FlashVD.read_reg_string('flashvideodownloaderlic', '0') == '0')
  612.             {
  613.                 FlashVD.showLic();
  614.                 //return;
  615.             }
  616.  
  617.             if(FlashVD.read_reg_string('flashvideodownloaderlic', '0') == '0')
  618.             {
  619.                 //FlashVD.showLic();
  620.                 return;
  621.             }
  622.  
  623.             var icon = document.getElementById('ila_tool_icon');
  624.  
  625.             // adult check
  626.             if (icon.getAttribute("adult_onpage")=='1')
  627.             {
  628.                 // 1. Check "dont't ask me again" state
  629.                 var adult_notification = true;
  630.                 try { adult_notification = FlashVD.options.getBoolPref ("adult_notification"); } catch(e) {}
  631.  
  632.                 // 2. Show warning
  633.                 if (adult_notification)
  634.                 {
  635.                     var params = {result:null};
  636.                     window.openDialog("chrome://flashvideodownloader/content/adult_notification.xul",null, "chrome,modal,centerscreen", params);
  637.                     if (params.result == null)
  638.                         return;
  639.                 }
  640.             }
  641.  
  642.             if (icon.getAttribute("isset_in_db")=='1')
  643.             {
  644.                 if (icon.getAttribute("video_onpage")=='1')
  645.                 {
  646.                     FlashVD.download_from_any_page();
  647.                 }
  648.                 else
  649.                 {
  650.                     // chek if cuurent address is main page
  651.                     if (icon.getAttribute("is_main_page")=='1')
  652.                         window.openDialog("chrome://flashvideodownloader/content/dialog.xul",null, "chrome,modal,centerscreen", null, "Warning! Downloading is impossible.", "You can't download this page", "Downloading from the Main page is disabled, please try another page.");
  653.                     else
  654.                     {
  655.                         // replaced by opening window in new tab
  656.                         //window.open('http://www.flashvideodownloader.org/download2.php?u='+content.document.location, "", "width=500,height=500,resizable=no");
  657.  
  658.                         // get the browser object
  659.                         var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  660.                                 .getService(Components.interfaces.nsIWindowMediator);
  661.                         var mainWindow = wm.getMostRecentWindow("navigator:browser");
  662.                         var browser = mainWindow.getBrowser();
  663.  
  664.                         // add new tab with our url
  665.                         var tab = browser.addTab('http://www.flashvideodownloader.org/download2.php?u='+content.document.location);
  666.  
  667.                         // make the tab active
  668.                         browser.selectedTab = tab;
  669.                     }
  670.                 }
  671.             }
  672.             else
  673.             {
  674.                 return window.openDialog("chrome://flashvideodownloader/content/send_form.xul",null,'chrome,centerscreen,dependent',window.content.document.location.host);
  675.             }
  676.         }
  677.         catch(e)
  678.         {
  679.             //alert("DownloadLink\n" + e);
  680.         }
  681.     },
  682.  
  683.     download_from_any_page : function ()
  684.     {
  685.         try
  686.         {
  687.             var mediaNodes = [];
  688.             var allUrls={};
  689.             var nodeByUrl={};
  690.             var serviceNeed={};
  691.  
  692.             var embs=window.content.document.getElementsByTagName('embed');
  693.             if(embs.length>0)
  694.             {
  695.                 for(var i=0;i<embs.length;i++)
  696.                 {
  697.                     var emb = embs[i];
  698.                     var url = null;
  699.  
  700.                     if(emb.src.match(/\.youtube\./i))
  701.                     {
  702.                         emb.src.match(/youtube.com\/v\/(.{11})/i);
  703.                         url = 'http://www.youtube.com/watch?v='+RegExp.$1;
  704.                     }
  705.                     else if(emb.src.match(/\.santabanta\./i))
  706.                     {
  707.                         emb.getAttribute('flashvars').match(/video_id=([\d]+)/i);
  708.                         url = 'http://www.santabanta.com/video.asp?video='+RegExp.$1;
  709.                     }
  710.                     else if(emb.src.match(/\.google\./i))
  711.                     {
  712.                         emb.src.match(/docid=([\d]+)/i);
  713.                         url = 'http://video.google.com/videoplay?docid='+RegExp.$1;
  714.                     }
  715.                     else if(emb.src.match(/player\.hulu\.com\/embed\/myspace_viral_player/i))
  716.                     {
  717.                         emb.src.match(/videoID=([\d]+)/i);
  718.                         url = 'http://mediaNodes.myspace.com/index.cfm?fuseaction=mediaNodes.individual&VideoID='+RegExp.$1;
  719.                     }
  720.                     else if(emb.src.match(/\.collegehumor\./i))
  721.                     {
  722.                         emb.src.match(/clip_id=([\d]+)/i);
  723.                         url = 'http://www.collegehumor.com/video:'+RegExp.$1;
  724.                     }
  725.                     else if(emb.src.match(/\.metacafe\./i))
  726.                     {
  727.                         emb.src.match(/fplayer\/([\d]+)\/(.*)\.swf/i);
  728.                         url = 'http://www.metacafe.com/watch/'+RegExp.$1+'/'+RegExp.$2+'/';
  729.                     }
  730.                     else if(emb.src.match(/\.dailymotion\./i))
  731.                     {
  732.                         emb.src.match(/swf\/([^&]+)/i);
  733.                         url = 'http://www.dailymotion.com/video/'+RegExp.$1;
  734.                     }
  735.                     else if(emb.src.match(/\.dada\.net/i))
  736.                     {
  737.                         emb.getAttribute('flashvars').match(/mediaID=([\d]+)/i);
  738.                         url = 'http://ru.dada.net/video/'+RegExp.$1+'/';
  739.                     }
  740.                     else if(emb.src.match(/\.redtube\./i))
  741.                     {
  742.                         emb.src.match(/\?id=([\d]+)/i);
  743.                         url = 'http://www.redtube.com/'+RegExp.$1;
  744.                     }
  745.  
  746.                     if (url != null && allUrls[url] == null)
  747.                     {
  748.                         allUrls[url]="";            // prevent duplicates
  749.                         mediaNodes.push(url);
  750.                         nodeByUrl[url] = emb.parentNode;
  751.                         serviceNeed[url] = true;
  752.                     }
  753.                 }
  754.             }
  755.  
  756.             //var mes;
  757.             //while(mes=mediaNodes.pop())alert(mes);
  758.  
  759.             var nodes = [];
  760.  
  761.             var rootUrl = Components.classes["@mozilla.org/network/standard-url;1"].createInstance(Components.interfaces.nsIURI);
  762.             rootUrl.spec = window.content.document.URL;
  763.  
  764.             var mediaPattern="mpg|mpeg|mp3|mp4|avi|rm|wmv|mov|flv|swf"; //r
  765.             var mPatt=new RegExp("^.*\\.(?:"+mediaPattern+")$","i");
  766.             var dom=window.content.document;
  767.  
  768.             var xpr=dom.evaluate(".//a[@href]", dom, null,
  769.                 5, // == Components.interfaces.nsIDOMXPathResult.ORDERED_NODE_ITERATOR_TYPE,
  770.                 null);
  771.             var node0=xpr.iterateNext();
  772.             while(node0!=null)
  773.             {
  774.                 var url=node0.getAttribute("href");
  775.                 if(mPatt.exec(url)!=null)
  776.                     nodes.push(node0);
  777.  
  778.                 node0=xpr.iterateNext();
  779.             }
  780.  
  781.             // Searching src
  782.             xpr=dom.evaluate(".//embed[@src]", dom, null,
  783.                 5, // == Components.interfaces.nsIDOMXPathResult.ORDERED_NODE_ITERATOR_TYPE,
  784.                 null);
  785.             node0=xpr.iterateNext();
  786.             while(node0!=null)
  787.             {
  788.                 var url=node0.getAttribute("src");
  789.                 if(mPatt.exec(url)!=null)
  790.                     nodes.push(node0);
  791.  
  792.                 node0=xpr.iterateNext();
  793.             }
  794.  
  795.             for(var i=0;i<nodes.length;i++)
  796.             {
  797.                 var node=nodes[i];
  798.                 var url=node.getAttribute("href");
  799.                 if (url == null)
  800.                 {
  801.                     url=node.getAttribute("src");
  802.                     if (url != null)
  803.                         node = node.parentNode;
  804.  
  805.                 }
  806.                 if(allUrls[url]!=null)
  807.                     continue;
  808.                 allUrls[url]="";
  809.                 if(mPatt.exec(url)==null)
  810.                     continue;
  811.  
  812.                 //var hrefParts=/^(.*[^0-9])?([0-9]+)([^\/]*?\.[^\/]*?)$/.exec(url);
  813.                 //if(hrefParts!=null && hrefParts.length==4)
  814.                 //{
  815.                 //    var ext = /.*\.(.*?)$/.exec(hrefParts[3])[1];
  816.                 //}
  817.  
  818.                 var urlStr=rootUrl.resolve(url);
  819.  
  820.                 mediaNodes.push(urlStr);
  821.                 nodeByUrl[urlStr] = node;
  822.             }
  823.  
  824.             if(mediaNodes.length>0)
  825.             {
  826.                 return window.openDialog("chrome://flashvideodownloader/content/pagevids.xul",null,'chrome,centerscreen,dependent,scrollbars=yes', rootUrl.spec, mediaNodes, nodeByUrl, serviceNeed);
  827.             }
  828.         }
  829.         catch(e)
  830.         {
  831.             //alert("download_from_any_page\n" + e);
  832.         }
  833.     },
  834.  
  835.  
  836.     tool_icon_menu : function(loc)
  837.     {
  838.         var hr='';
  839.         if(loc=='h')hr='http://www.flashvideodownloader.org/helpfirefox.php';
  840.         if(loc=='c')hr='http://www.flashvideodownloader.org/fvd-suite/';
  841.         if(loc=='f')hr='http://www.flashvideodownloader.org/flash-converter/contact/index.php';
  842.         window.content.document.location.href=hr;
  843.     },
  844.  
  845.     show_options : function()
  846.     {
  847.         return window.openDialog("chrome://flashvideodownloader/content/options.xul",null,'chrome,centerscreen,toolbar,dependent',false);
  848.     },
  849.  
  850.     search_video : function()
  851.     {
  852.         return window.openDialog("chrome://flashvideodownloader/content/searchvideo.xul",null,'chrome,centerscreen,dependent',false);
  853.     },
  854.  
  855.     myLoadHandler : function(e)
  856.     {
  857.         FlashVD.init();
  858.  
  859.         var hotk;
  860.         try
  861.         {
  862.             var hotk=FlashVD.options.getCharPref('hotkey');
  863.             var hotm=FlashVD.options.getCharPref('hot_mod');
  864.         }catch(e){return;}
  865.         window.removeEventListener(e.type, arguments.callee, false);
  866.         document.getElementById("ila_hotkey").setAttribute("key",hotk);
  867.         document.getElementById("ila_hotkey").setAttribute("modifiers",hotm);
  868.     }
  869.  
  870. } // FlashVD
  871.  
  872. window.addEventListener("load", FlashVD.myLoadHandler, false);
  873. window.document.addEventListener("load", FlashVD.set_icon, true);
  874. window.addEventListener("load", FlashVD.ila_load, false);
  875. //window.document.addEventListener("DOMContentLoaded", set_icon, false);